package com.pearadmin.pro.modules.mkt.service.impl;

import java.io.Serializable;
import java.util.*;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.base.page.Pageable;

import com.pearadmin.pro.modules.mkt.domain.MktActive;
import com.pearadmin.pro.modules.mkt.domain.MktContractDetail;
import com.pearadmin.pro.modules.mkt.param.MktActiveRequest;
import com.pearadmin.pro.modules.mkt.repository.MktActiveRepository;
import com.pearadmin.pro.modules.mkt.repository.MktContractDetailRepository;
import com.pearadmin.pro.modules.mkt.service.MktActiveService;
import com.pearadmin.pro.modules.mkt.service.MktContractDetailService;
import com.pearadmin.pro.modules.school.domain.Contacts;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.swing.text.html.Option;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;


/**
 * 市场活动Service业务层处理
 *
 * @author chenJian
 * @date 2021-09-09
 */
@Service
public class MktActiveServiceImpl extends ServiceImpl<MktActiveRepository, MktActive> implements MktActiveService {
    @Resource
    private MktActiveRepository mktActiveRepository;

    @Autowired
    private MktActiveService mktActiveService;

    @Autowired
    private MktContractDetailService mktContractDetailService;

    @Autowired
    private MktContractDetailRepository mktContractDetailRepository;

    /**
     * 查询市场活动
     *
     * @param request
     * @return 市场活动 分页集合
     */
    @Override
    public PageResponse<MktActive> page(MktActiveRequest request) {
        LambdaQueryWrapper<MktActive> query = new LambdaQueryWrapper<MktActive>();
        query.eq(ObjectUtil.isNotEmpty(request.getActType()), MktActive::getActType, request.getActType());
        query.like(ObjectUtil.isNotEmpty(request.getActName()), MktActive::getActName, request.getActName());
        query.gt(ObjectUtil.isNotEmpty(request.getBgeinTime()), MktActive::getBgeinTime, request.getBgeinTime());
        query.lt(ObjectUtil.isNotEmpty(request.getEndTime()), MktActive::getEndTime, request.getEndTime());
        query.eq(ObjectUtil.isNotEmpty(request.getActStatus()), MktActive::getActStatus, request.getActStatus());
        query.like(ObjectUtil.isNotEmpty(request.getActHead()), MktActive::getActHead, request.getActHead());
        return Pageable.of(request, (() -> mktActiveRepository.selectList(query)));
    }

    /**
     * 查询市场活动
     *
     * @param id 市场活动ID
     * @return 市场活动
     */
    @Override
    public MktActive selectMktActiveById(Long id) {
        MktActive mktActive = mktActiveRepository.selectById(id);
        LambdaQueryWrapper<MktContractDetail> wr = new LambdaQueryWrapper<>();
        wr.eq(ObjectUtil.isNotEmpty(mktActive.getId()), MktContractDetail::getActtiveId, mktActive.getId());
        List<MktContractDetail> mktContractDetails = mktContractDetailRepository.selectList(wr);
        mktActive.setContractDetails(mktContractDetails);
        return mktActive;
    }

    @Override
    public boolean actSaveOrUpdate(MktActive mktActive) {
        mktActiveService.saveOrUpdate(mktActive);
        if (StringUtils.isNotBlank(mktActive.getContractDetail())) {//市场活动明细信息
            JSONArray jsonArray = (JSONArray) JSONArray.parse(mktActive.getContractDetail());
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                Object id = jsonObject.get("id");
                if (id.equals("0")) {
                    MktContractDetail mdetail1 = new MktContractDetail();
                    mdetail1.setActtiveId(mktActive.getId());
                    mdetail1.setDemand(jsonObject.get("demand").toString());
                    mdetail1.setTagPeople(jsonObject.get("tagPeople").toString());
                    mdetail1.setTagTime(new Date());
                    mktContractDetailService.save(mdetail1);
                } else {
                    MktContractDetail mdetail2 = mktContractDetailRepository.selectById((Serializable) id);
                    mdetail2.setDemand(jsonObject.get("demand").toString());
                    mdetail2.setTagPeople(jsonObject.get("tagPeople").toString());
                    mktContractDetailRepository.updateById(mdetail2);
                }
            }
        }

        return true;
    }


}
